home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / test / test9.c < prev   
C/C++ Source or Header  |  1990-07-23  |  4KB  |  270 lines

  1. /* Test 9 setjmp with register variables.        Author: Ceriel Jacobs */
  2.  
  3. #include <setjmp.h>
  4. #include <signal.h>
  5. #include <stdio.h>
  6.  
  7. #define MAX_ERROR 4
  8.  
  9. int whichtest;
  10. int nerrors;
  11. int errct;
  12. int subtest = 1;
  13. char *tmpa;
  14.  
  15.  
  16. extern int errno;
  17.  
  18. main()
  19. {
  20.   jmp_buf envm;
  21.   register int i;
  22.   int j;
  23.  
  24.   printf("Test  9 ");
  25.   for (j = 0; j < 100; j++) {
  26.     test1();
  27.     test2();
  28.     test3();
  29.     test4();
  30.     test5();
  31.     test6();
  32.   }
  33.   if (nerrors) {
  34.     printf("%d errors detected\n", nerrors);
  35.     exit(nerrors);
  36.   }
  37.   i = 1;
  38.   if (setjmp(envm) == 0) {
  39.     i = 2;
  40.     longjmp(envm, 1);
  41.   } else {
  42.     if (i == 2) {
  43.         /* Correct */
  44.     } else if (i == 1) {
  45.         printf("WARNING: The setjmp/longjmp of this machine restore register variables\n\
  46. to the value they had at the time of the \"setjmp\"\n");
  47.     } else {
  48.         printf("Aha, I just found one last error\n");
  49.         return 1;
  50.     }
  51.   }
  52.   printf("ok\n");
  53. }
  54.  
  55.  
  56. test1()
  57. {
  58.   register p;
  59.  
  60.   whichtest = 1;
  61.   p = 200;
  62.   garbage();
  63.   if (p != 200) e(1);
  64. }
  65.  
  66. test2()
  67. {
  68.   register p, q;
  69.  
  70.   whichtest = 2;
  71.   p = 200;
  72.   q = 300;
  73.   garbage();
  74.   if (p != 200) e(1);
  75.   if (q != 300) e(2);
  76. }
  77.  
  78. test3()
  79. {
  80.   register p, q, r;
  81.  
  82.   whichtest = 3;
  83.   p = 200;
  84.   q = 300;
  85.   r = 400;
  86.   garbage();
  87.   if (p != 200) e(1);
  88.   if (q != 300) e(2);
  89.   if (r != 400) e(3);
  90. }
  91.  
  92. char buf[512];
  93.  
  94. test4()
  95. {
  96.   register char *p;
  97.  
  98.   whichtest = 4;
  99.   p = &buf[100];
  100.   garbage();
  101.   if (p != &buf[100]) e(1);
  102. }
  103.  
  104. test5()
  105. {
  106.   register char *p, *q;
  107.  
  108.   whichtest = 5;
  109.   p = &buf[100];
  110.   q = &buf[200];
  111.   garbage();
  112.   if (p != &buf[100]) e(1);
  113.   if (q != &buf[200]) e(2);
  114. }
  115.  
  116. test6()
  117. {
  118.   register char *p, *q, *r;
  119.  
  120.   whichtest = 6;
  121.   p = &buf[100];
  122.   q = &buf[200];
  123.   r = &buf[300];
  124.   garbage();
  125.   if (p != &buf[100]) e(1);
  126.   if (q != &buf[200]) e(2);
  127.   if (r != &buf[300]) e(3);
  128. }
  129.  
  130. jmp_buf env;
  131.  
  132. /*    return address of local variable.
  133.   This way we can check that the stack is not polluted.
  134. */
  135. char *
  136.  addr()
  137. {
  138.   char a;
  139.  
  140.   return &a;
  141. }
  142.  
  143. garbage()
  144. {
  145.   register i, j, k;
  146.   register char *p, *q, *r;
  147.   char *a;
  148.   int t;
  149.  
  150.   p = &buf[300];
  151.   q = &buf[400];
  152.   r = &buf[500];
  153.   i = 10;
  154.   j = 20;
  155.   k = 30;
  156.   switch (setjmp(env)) {
  157.       case 0:
  158.     a = addr();
  159.     longjmp(env, 1);
  160.     break;
  161.       case 1:
  162.     if (i != 10) e(11);
  163.     if (j != 20) e(12);
  164.     if (k != 30) e(13);
  165.     if (p != &buf[300]) e(14);
  166.     if (q != &buf[400]) e(15);
  167.     if (r != &buf[500]) e(16);
  168.     tmpa = addr();
  169.     if (a != tmpa) e(17);
  170.     level1();
  171.     break;
  172.       case 2:
  173.     if (i != 10) e(21);
  174.     if (j != 20) e(22);
  175.     if (k != 30) e(23);
  176.     if (p != &buf[300]) e(24);
  177.     if (q != &buf[400]) e(25);
  178.     if (r != &buf[500]) e(26);
  179.     tmpa = addr();
  180.     if (a != tmpa) e(27);
  181.     level2();
  182.     break;
  183.       case 3:
  184.     if (i != 10) e(31);
  185.     if (j != 20) e(32);
  186.     if (k != 30) e(33);
  187.     if (p != &buf[300]) e(34);
  188.     if (q != &buf[400]) e(35);
  189.     if (r != &buf[500]) e(36);
  190.     tmpa = addr();
  191.     if (a != tmpa) e(37);
  192.     hard();
  193.       case 4:
  194.     if (i != 10) e(41);
  195.     if (j != 20) e(42);
  196.     if (k != 30) e(43);
  197.     if (p != &buf[300]) e(44);
  198.     if (q != &buf[400]) e(45);
  199.     if (r != &buf[500]) e(46);
  200.     tmpa = addr();
  201.     if (a != tmpa) e(47);
  202.     return;
  203.     break;
  204.       default:    e(100);
  205.   }
  206.   e(200);
  207. }
  208.  
  209. level1()
  210. {
  211.   register char *p;
  212.   register i;
  213.  
  214.   i = 1000;
  215.   p = &buf[10];
  216.   i = 200;
  217.   p = &buf[20];
  218.   longjmp(env, 2);
  219. }
  220.  
  221. level2()
  222. {
  223.   register char *p;
  224.   register i;
  225.  
  226.   i = 0200;
  227.   p = &buf[2];
  228.   *p = i;
  229.   dolev();
  230. }
  231.  
  232. dolev()
  233. {
  234.   register char *p;
  235.   register i;
  236.  
  237.   i = 010;
  238.   p = &buf[3];
  239.   *p = i;
  240.   longjmp(env, 3);
  241. }
  242.  
  243. catch()
  244. {
  245.   longjmp(env, 4);
  246. }
  247.  
  248. hard()
  249. {
  250.   register char *p;
  251.  
  252.   signal(SIGHUP, catch);
  253.   for (p = buf; p <= &buf[511]; p++) *p = 025;
  254.   kill(getpid(), SIGHUP);
  255. }
  256.  
  257. e(n)
  258. int n;
  259. {
  260.   int err_num = errno;        /* save errno in case printf clobbers it */
  261.  
  262.   printf("Subtest %d,  error %d  errno=%d  ", subtest, n, errno);
  263.   errno = err_num;        /* restore errno, just in case */
  264.   perror("");
  265.   if (errct++ > MAX_ERROR) {
  266.     printf("Too many errors; test aborted\n");
  267.     exit(1);
  268.   }
  269. }
  270.